//
//  WCNLoginVC.m
//  WCN
//
//  Created by 杨鑫 on 15/5/29.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//


#import "HELLoginVC.h"
//VC
#import "HELForgotPwdVC.h"
#import "NSString+Utilities.h"
#import "WCBorderView.h"
#import "HELTabBarVC.h"
#import "HELNavigationContrller.h"
#import "AppDelegate.h"
#import <UMMobClick/MobClick.h>
#import "WCBaiduPushBindHelp.h"
#import <Realm/Realm.h>
#import "ReamlDataTool.h"

@interface HELLoginVC () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet WCBorderView *phoneBgView;

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;

@end

@implementation HELLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
//    self.phoneTextField.text = [[WCDefaultManager shareMg] lastAccount];
    self.pwdTextField.delegate = self;
    

    [self titleView:@"登录"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark - Nib Targets
//click backgroundView
- (IBAction)bgTouchDown:(id)sender
{
    
//    [self.phoneTextField hideList];
    [self.view endEditing:YES];
}

//密码框  是否安全显示
- (IBAction)togglePwdBtn:(UIButton *)sender
{

    BOOL shouldBecomeFirstResponder = self.pwdTextField.isFirstResponder?YES:NO;
    BOOL selected = sender.selected;
    
    [sender setSelected:!selected];
    self.pwdTextField.enabled = NO;
    self.pwdTextField.secureTextEntry = selected;
    self.pwdTextField.enabled = YES;
    
    if (shouldBecomeFirstResponder) {
        [self.pwdTextField becomeFirstResponder];
    }
}

//忘记密码
- (IBAction)showPwdVC:(id)sender {
    HELForgotPwdVC *pwdVC = [HELForgotPwdVC new];
    
    [self.navigationController pushViewController:pwdVC animated:YES];
}

// 点击登录
- (IBAction)doLogin:(id)sender {
    [self.view endEditing:YES];
    NSString *userAcount = self.phoneTextField.text, *password = self.pwdTextField.text;
    if (!([userAcount isMobile])) {
        alertMessage(@"手机格式不正确～");
        return;
    }
    if ([userAcount isEmpty]) {
        alertMessage(@"账号不能为空～");
        return;
    }

    if ([password isEmpty]) {
        alertMessage(@"密码不能为空～");
        return;
    }
    
    cwqHud(self.view);
    
    NSDictionary *param = @{@"j_username":userAcount, @"j_password":password};
    [[HELNetworkManager sharedMg] POST:helLoginUrl parameters:param success:^(id responseObject) {
        if (responseObject[@"exception"]) {
            cwqHudRequestError(responseObject[@"exception"]);
        }else {
            
            [HELAppManager saveUseInfo:responseObject[@"user"]];
//            [MobClick profileSignInWithPUID:responseObject[@"user"][@"userId"]];
            [MobClick event:@"LoginCount" label:deviceName];
            
            cwqHudSuccess(hud);
            HELTabBarVC *tabVC = [[HELTabBarVC alloc] init];
//            HELNavigationContrller *nav = [[HELNavigationContrller alloc] initWithRootViewController:tabVC];
////            [nav firstNav];
//            [HELAppManager sharedAppManger].appNav = nav;
            AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            
            delegate.window.rootViewController = tabVC;
            //百度云推绑定后台
             [WCBaiduPushBindHelp bindToService];
            
            [[ReamlDataTool sharedTool] configWithConfig:userAcount];
        }
        
    } failure:^(NSString *err) {
       cwqHudRequestError(err);
    }];
}



- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

#pragma mark - Notification

- (void)keyboardWillHide:(NSNotification *)note
{
    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:7<<16
                     animations:^{
                         self.view.transform = CGAffineTransformIdentity;
                     } completion:NULL];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    
    CGRect rect = [textField convertRect:textField.bounds toView:self.view];
    CGFloat y = self.view.frame.size.height - rect.origin.y - rect.size.height - kKeyboardHeight;
    
#warning 为了配合键盘弹出的动画，使用7<<16.
    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:7<<16
                     animations:^{
                         self.view.transform = CGAffineTransformMakeTranslation(0, y>=0?0:y-10);
                     } completion:NULL];
    
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    if (textField.tag == 0) {
        self.pwdTextField.text = @"";
    }
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == 1) {
        [textField resignFirstResponder];
        [self doLogin:nil];
        return YES;
    }
    
    return NO;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag ==0) {
//        [self showSuggestList];
    }
}
@end
